package com.pzr.soccer.match.service.impl;

import com.pzr.soccer.common.aop.MetricTime;
import com.pzr.soccer.match.dao.GameRepository;
import com.pzr.soccer.match.domain.Game;
import com.pzr.soccer.match.domain.MatchInfo;
import com.pzr.soccer.match.model.request.DateLT;
import com.pzr.soccer.match.model.request.PreConditions;
import com.pzr.soccer.match.model.request.ResultConditions;
import com.pzr.soccer.match.service.GameService;
import com.pzr.soccer.match.util.FindRightGameForExpressions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @Author pzr
 * @date:2021-12-15-18:14
 * @Description:
 **/
@Service
public class GameServiceImpl implements GameService {
    @Autowired
    GameRepository repository;

    @MetricTime("find by dlt")
    @Override
    public List<Game> findByDateLT(DateLT dlt) {
        System.out.println("begin find by dlt");
        return repository.findByDateLT(dlt);
    }

    @Override
    @MetricTime("select match games")
    public List<Game> selectMatchIndex(PreConditions pc,List<Game> games) {
        return games.stream().filter(game -> FindRightGameForExpressions.testExpressions(game,0,pc.getUntil(),pc.getExpressions())).collect(Collectors.toList());
    }

    @Override
    public List<Game> selectMatchResult(ResultConditions rc, List<Game> games) {
        return games.stream().filter(game -> FindRightGameForExpressions.testExpressions(game,rc.getBegin(),rc.getEnd(),rc.getExpressions())).collect(Collectors.toList());
    }

    @Override
    public List<Game> covertOldFormatGame2New(List<MatchInfo> matchInfos) {
        return matchInfos.stream().map(this::convert2game).collect(Collectors.toList());
    }

    private NavigableMap<Integer,int[]> convertPairs(NavigableMap<Integer,List<Integer>> oldMap){
        NavigableMap<Integer,int[]> newMap=new TreeMap<>();
        for (Map.Entry<Integer, List<Integer>> entry : oldMap.entrySet()) {
            int[] arr=new int[2];
            arr[0]=entry.getValue().get(0);
            arr[1]=entry.getValue().get(1);
            //大于等于47分钟的（默认下半场），加100
            newMap.put(entry.getKey()>=47?entry.getKey()+100:entry.getKey(),arr);
        }
        return newMap;
    }

    private Game convert2game(MatchInfo matchInfo){
        Game game = new Game();
        game.setId(matchInfo.getId());
        String timeStr=matchInfo.getDate().toString()+"T"+matchInfo.get开赛时间()+":00";
        game.setKickOff(LocalDateTime.parse(timeStr));
        game.setStrongTeam(matchInfo.getStrongTeam());
        game.setAway(matchInfo.get客队());
        game.setHome(matchInfo.get主队());
        game.setCorner(matchInfo.getCorner());
        game.setDanger(matchInfo.getDangerous());
        game.setDaXiaoPan(matchInfo.getDaXiaoPan());
        game.setDaXiao(matchInfo.getDaXiao());
        game.setHalfCorner(matchInfo.getHalfCorner());
        game.setLeague(matchInfo.get联赛());
        game.setScore(matchInfo.getScore());
        game.setShot(matchInfo.getShot());
        game.setJiaoQiuDaXiao(matchInfo.getJiaoQiuDaXiao());
        game.setJiaoQiuPan(matchInfo.getJiaoQiuPan());

        game.setPairCorner(convertPairs(matchInfo.getPairCorner()));
        game.setPairDanger(convertPairs(matchInfo.getPairDangerous()));
        game.setPairScore(convertPairs(matchInfo.getPairScore2()));
        game.setPairShot(convertPairs(matchInfo.getPairShot()));

        return game;
    }
}
